import world
from pos import Position

from log import getLogger
logger=getLogger('Bomb')

from actor import Extended,Actor
from event import Event

import stackless
import time

class DestroyAble:
    pass

class Wall:
    pass

class PickAble:
    pickable=True


class Bomb(Extended,DestroyAble):

    def __init__(self,world_,pos,timeup=5,size=1):
        Extended.__init__(self,world_,pos)

        self.timeup=timeup
        logger.debug(self.timeup)
        self.bombed=False
        self._size=size

    def bomb(self,atOnce=False):
        logger.debug(self.timeup)
        if not atOnce :
            self.world.sleepManager.sleep(self.timeup)

        if self.bombed :
            logger.debug('destroyed?!')
            return

        bombed=1

        self.remove()

        self.makeExplotion()
    
    def makeExplotion(self):
        
        #x line
        for x in range(self._size+1):
            if self.setExplotionPos(Position(self.pos.x+x,self.pos.y)):
                break

        for x in range(self._size+1):
            if self.setExplotionPos(Position(self.pos.x-x,self.pos.y)):
                break

        #y line
        for y in range(self._size+1):
            if self.setExplotionPos(Position(self.pos.x,self.pos.y+y)):
                break

        for y in range(self._size+1):
            if self.setExplotionPos(Position(self.pos.x,self.pos.y-y)):
                break

    def setExplotionPos(self,pos):

        collisions=self.world.getCollisions(pos)

        logger.debug(collisions)

        for obj in collisions:
            if isinstance(obj,world.World):
                return 1
            elif isinstance(obj,Wall):

                if isinstance(obj,DestroyAble):
                    Explotion(self.world,pos)
                    return -1

                else :
                    return 1

        Explotion(self.world,pos)

    def destroyed(self,obj):
        self.bomb(atOnce=True)
        self.bombed=True

    action=bomb

class Explotion(Extended):

    def __init__(self,*args,**kwargs):
        Extended.__init__(self,*args,**kwargs)


    def explode(self):

        for obj in self.world.getCollisions(self.pos,obj=self):

            logger.debug(obj)


            if isinstance(obj,Actor):
                stackless.tasklet(obj.channel.send)(Event(subject='destroy',obj=self))
            elif isinstance(obj,DestroyAble) :
                stackless.tasklet(obj.destroyed)(obj=self)

        self.world.sleepManager.sleep(0.2)

        self.remove()
        del self

    action=explode

class UnDestroyableWall(Extended,Wall):
    pass

class DestroyableWall(Extended,Wall,DestroyAble):

    def destroyed(self,obj):
        self.remove()
        del self

class Gold(Extended,PickAble):

    def __init__(self,world_,pos,value=10):
        Extended.__init__(self,world_,pos)

        self.value=value
        self.IsGold=True

    def picked(self,obj):

        stackless.tasklet(obj.channel.send)(Event(subject='get',obj=self))

        self.remove()

class Exit(Extended,PickAble):

    def picked(self,obj):

        if not self.world.gold :
            stackless.tasklet(self.world.channel.send)(Event(subject='stage_clear'))


